package com.sean.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

@Configuration
@EnableOpenApi
public class SwaggerConfig {

    @Bean
    public Docket docket1() {
        return new Docket(new DocumentationType(DocumentationType.OAS_30.getName(),
                DocumentationType.OAS_30.getVersion(),
                DocumentationType.OAS_30.getMediaType()))
                .groupName("A");
    }

    @Bean
    public Docket docket2() {
        return new Docket(new DocumentationType(DocumentationType.OAS_30.getName(),
                DocumentationType.OAS_30.getVersion(),
                DocumentationType.OAS_30.getMediaType()))
                .groupName("B");
    }

    @Bean
    public Docket docket3() {
        return new Docket(new DocumentationType(DocumentationType.OAS_30.getName(),
                DocumentationType.OAS_30.getVersion(),
                DocumentationType.OAS_30.getMediaType()))
                .groupName("C");
    }

    @Bean
    public Docket docket(Environment environment) {
        Profiles profiles = Profiles.of("dev", "test");
        boolean flag = environment.acceptsProfiles(profiles);


        return new Docket(new DocumentationType(DocumentationType.OAS_30.getName(),
                DocumentationType.OAS_30.getVersion(),
                DocumentationType.OAS_30.getMediaType()
                ))
                .apiInfo(apiInfo())
                .groupName("小曹")
                .enable(flag)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.sean.controller"))
//                .paths(PathSelectors.ant("/sean/**"))
                .build();
    }

    private ApiInfo apiInfo() {
        Contact contact = new Contact("曹洪涛", "https://baidu.com", "sean@foxmail.com");
        return new ApiInfo("我的Swagger日记",
                "我的第一个Swagger日记，献给我唯一的爱人",
                "1.0",
                "https://baidu.com",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());
    }
}
